<?php    
$dir = dirname(realpath(__FILE__));	
require_once($dir.'/../src/kpapiv3.php');

class controller{
	private $publisher = 'YOUR PUBLISHER NAME'; // i.e byag
	private $devKey = 'YOUR DEV KEY';
		
	function __construct($userId,$userToken){
		
			
		$token = new KP_AuthToken($this->devKey);
		$token->publisher = $this->publisher;
		$token->setToken($userId,$userToken);
		
		$this->connection = new KP_Connection($token);
		$this->helper = new KP_HELPER();
		
		$type = isset($_GET['type'])?strtolower($_GET['type']):'home';
		$id = isset($_GET['id'])?strtolower($_GET['id']):NULL;
		
		switch($type){
			case 'home':
				$this->courses();
				break;
			case 'ou':
				$this->ou($id);
				break;
			case 'unitplan':
				$this->up($id);
				break;
			case 'page':
				$this->page($id);
				break;
			case 'practice':
				$this->practice($id);
				break;
			case 'test':
				$this->test($id);
				break;
			case 'grade':
				$this->grade($id);
				break;
			default:
				$this->courses();
				break;
		}
	}	
	
	protected function courses(){
		$courses = $this->connection->getCourses();
		echo '<ol>';
		foreach ($courses as $teacherName=>$value) {
			echo '<li>'.$teacherName;
			foreach($value as $class){
				$this->treeToList($class->tree);
			}
			echo '</li>';
		}
		echo '</ol>';		
	}
	
	protected function ou($id){
		$ou = $this->connection->getOU($id);
		echo sprintf('<article><h3>%s</h3>',$ou->cn);
		echo '<section><h4>Content</h4>';
		$this->treeToList($ou->meta->tree);
		echo '</section>';
		echo '<section><h4>Messages</h4>';
		$messages = $this->connection->getMessages($id);
		foreach($messages as $message){
			echo sprintf('<div><h5>%s</h5>%s<time>%s</time></div>',$message->format->teacher,$message->cn,$message->format->time);
		}
		echo '</section>';
		echo '</article>';
		
	}
	
	protected function up($id){
		$up = $this->connection->getUP($id);
		echo sprintf('<article><h3>%s</h3>',$up->cn);
		echo sprintf('<section><h4>Objectives</h4>%s<ul>',$up->competency);		
		foreach($up->related->objectives->objectives as $objective){
			echo sprintf('<li>%s</li>',$objective->value);
		}
		echo '</ul></section>';
		echo '<section><h4>Pages</h4>';
		foreach($up->related->pages->pages as $page){			
			$pageId =$this->helper->urnToId($page->_id);
			echo sprintf('<li><a href="/%s?type=page&id=%s">%s</a></li>',basename(__FILE__),$pageId,$page->cn);
		}
		echo '</section>';
		echo '<section><h4>Testing</h4>';
		echo sprintf('<a href="%s?type=practice&id=%s">Take a Practice Test</a>',basename(__FILE__),$id);
		echo '</section>';
		echo '</article>';		
	}
	
	protected function page($id){
		$page = $this->connection->getPage($id);
		echo sprintf("<article><h3>%s</h3><section>%s</section></article>",$page->cn,$this->helper->processHtml($page->content,basename(__FILE__)));
	}
	
	protected function practice($id){
		$practice = $this->connection->getPractice($id);
		$this->questionsToTest($id, 'practice', $practice);		
	}
	
	protected function test($id){
		$test = $this->connection->getAssessment($id);
		$this->questionsToTest($id, 'test', $test);		
	}

	protected function grade($id){		
		$sub = isset($_GET['sub'])?strtolower($_GET['sub']):'practice';
		$result = $this->connection->gradeTest($id,$_POST);
				
		echo '<article><h3>Results</h3>';
		if($sub === 'practice'){
			$level = $this->helper->aptitudeToLevel($result->percentage);
			echo sprintf('<p>You have achieved %s level of understanding on this practice test.</p>',$level);
			echo sprintf('<a href="%s?type=unitplan&id=%s">Improve your knowledge</a>',basename(__FILE__),$id);
		}else{
			$scoreOnCert = $result->percentage;
			//TODO do something with $scoreOnCert;
			//load what ever reward you want to.
		}
		echo '</article>';
	}
	
	private function questionsToTest($id,$sub,$questions){
		echo sprintf('<form method="POST" action="%s?type=grade&id=%s&sub=%s">',basename(__FILE__),$id,$sub);
		echo '<ol>';
		foreach($questions as $question){
			echo sprintf('<li>"%s"',$question->cn);
			echo '<ol>';
			
			//$questionId = $this->helper->urnToId($question->_id);
			foreach($question->choices as $choice){				
				echo sprintf('<li><input type="radio" name="%s" value="%s"/>%s</li>',$question->_id,$choice,$choice);
			}
			echo sprintf('<input type="hidden" name="%s" value="NA"/>',$question->_id);			
			echo '</ol>';
			echo '</li>';	
		}
		echo '</ol>';
		echo '<input type="submit" />';
		echo '</form>';
	}
	
	private function treeToList($tree){
		echo '<ol>';
		echo sprintf('<a href="/%s?type=%s&id=%s">%s</a>',basename(__FILE__),$tree->type,$tree->id,$tree->cn);
		if(isset($tree->children) && count($tree->children)>0){
			foreach($tree->children as $child){
				$this->treeToList($child);
			}
		}
		echo '</ol>';
	}
}

/**
 * Fake DB that should be replaced by your permanent storage methodology
 */
$fakeDB = new stdClass;
$fakeDB->c3R1ZGVudEBrcGNvbXBhc3MuY29t = 'eyJ1c2VyIjogeyJwaWN0dXJlIjogIiIsICJjbiI6ICJKYXNtaW5lIERvbmFsZCIsICJyb2xlcyI6IFsic3R1ZGVudCJdLCAiYXV0aCI6ICIwUTllMWF6Tks5cGVaQ2VCL01IaHg1bkNvZlcwTUJTTXM5WkhYT21SNHloS25mdXZnUU4vbzF0L0NwaksxVlMvSnlkRVRrc0I1eEdpbEhIWXFkQ2xJdz09IiwgInRlYWNoZXJzIjogWyJibUZwIiwgIloyaGxibVJ5ZVVCbGMzVXVaV1IxIiwgImJXRnlZV0pzWldweVFHMWpjMnN4TWk1dVpYUSIsICJaR3gzYjI5a2MwQnRjSE5oZWk1dmNtYyIsICJkR1Z5Y21Wc2JHUmhibXhsZVVCbmJXRnBiQzVqYjIwIiwgIlpHVmljbUZ0YjI5eVpVQnJjR052YlhCaGMzTXVZMjl0Il0sICJjb3VudHMiOiB7ImJvb2tzIjogMCwgInRhdWdodCI6IDAsICJzdHVkZW50IjogNn0sICJpZCI6ICJjM1IxWkdWdWRFQnJjR052YlhCaGMzTXVZMjl0In0sICJjaGVjayI6ICJiYmIzZDQ3ZGQ5M2E2MzkxNmNkNTJhZjMzNmU1MGU5ZiJ9'; 

/**
 * This should actually be replaced method to get your real user id
 */
$idRetrievedFromActiveUser = 'c3R1ZGVudEBrcGNvbXBhc3MuY29t';  
$controller = new controller($idRetrievedFromActiveUser,$fakeDB->{$idRetrievedFromActiveUser});
		
?>
